<template>
  <div class="base" v-if="showLogin">
  <div class="a">
      <div class="app1">
        <div class="logo">系统登录</div>
        <div class="input-group">
          <label class="input-label">用户名</label>
          <input type="text" v-model="username" placeholder="用户名" class="input-filed">
        </div>
        <div class="input-group">
          <label class="input-label">密码</label>
         <input type="password" v-model="password" placeholder="密码" class="input-filed">
        </div>

        <div class="error-message" v-if="errorMessage">{{ errorMessage }}</div>

        <div class="but">
          <button @click="reg" class="btn">注册</button>
          <button @click="login" class="btn">登录</button>
        </div>
      </div>
    </div>
  </div>
  <div v-else>
    <router-view />
  </div>

</template>

<script>
  export default {
    name: 'LoginApp',
    data() {
      return {
        showLogin: true,
        username: '',
        password: '',
        errorMessage: '',
        list: Array(15).fill(''), 
        itemPerRow: 3
      }
    },
    mounted() {
    const storedState = sessionStorage.getItem('showLogin');
    this.showLogin = storedState !== '0' || (this.$route.path === '/' || this.$route.path === '/login');
},
    methods: {
      isOddRow(index) {
        const rowIndex = Math.floor(index / this.itemPerRow) + 1;
        return rowIndex % 2 === 1;
      },
      login() {
        if (!this.username.trim()) {
          this.errorMessage = '请输入用户名';
          return;
        }
        
        if (!this.password || this.password.length < 6) {
          this.errorMessage = '密码长度至少 6 位';
          return;
        }
        
        this.showLogin = false;
        sessionStorage.setItem('showLogin', '0');
        
        this.$router.push('/Databind')
          .catch(error => {
            console.error('登录跳转失败:', error);
            this.errorMessage = '登录失败，请重试';
          });
        
        this.username = '';
        this.password = '';
        this.errorMessage = '';
    },
    reg() {
      this.showLogin = false; 
      sessionStorage.setItem('showLogin', '0');
      
       this.$router.push('/halo')
        .catch(error => {
          console.error('注册跳转失败:', error);
          this.errorMessage = '注册页面加载失败，请重试';
        });
    }
  }
}
</script>

<style scoped>
  .input-group {
    margin-bottom: 30px;
  }


  .input-label {
    display: block;
    margin-bottom: 8px;
    color: rgb(11, 117, 178);
    font-size: 16px;
    font-weight: 500;
    padding-left: 4px;
  }


  .input-field {
    width: 100%;
    padding: 20px 20px;
    border: none;
    border-radius: 40px;
    background: rgba(46, 160, 189, 0.2);
    font-size: 16px;
    color: rgb(210, 241, 243);
    transition: all 0.3s;
    box-shadow: inset 0 2px 5px rgba(12, 98, 237, 0.1);
  }

  .base {
    background-color: #E0F7FA 100%;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
  }

  .base::before {
    content: "";
    position: absolute;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    background: rgba(4, 124, 188, 0.942);
    top: -150px;
    right: -150px;
    filter: blur(100px);
  }

  .base::after {
    content: "";
    position: absolute;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    background: rgba(4, 124, 188, 0.942);
    bottom: -150px;
    left: -150px;
    filter: blur(100px);
  }

  .login-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1;
  }

  .a {
  width: 100%; 
  display: flex;
  justify-content: center; 
  }

  .app1 {
    display: flex;
    flex-direction: column;
    width: 380px;
    margin: 0 auto; 
    padding: 40px;
    background: linear-gradient(145deg, rgba(0, 138, 197, 0.8), rgba(0, 138, 197, 0.6));
    border-radius: 20px;
    box-shadow: 0 15px 35px rgba(0, 138, 197, 0.3), 0 5px 15px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
    transition: transform 0.3s, box-shadow 0.3s;
  }

  .app1:hover {
    transform: translateY(-5px);
    box-shadow: 0 20px 40px rgba(0, 138, 197, 0.4), 0 8px 25px rgba(0, 0, 0, 0.15);
  }

  .logo {
    font-size: 28px;
    font-weight: 700;
    color: white;
    text-align: center;
    margin-bottom: 30px;
    letter-spacing: 1px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }

  .input-group {
  width: 100%;
  margin-bottom: 30px;
  }

  .input-field {
    width: 100%; 
    margin-bottom: 30px;
    padding: 18px 20px;
    border: none;
    border-radius: 12px;
    background: rgba(46, 160, 189, 0.2);
    font-size: 16px;
    color: white;
    transition: all 0.3s;
    box-shadow: inset 0 2px 5px rgba(12, 98, 237, 0.1);
  }

  .input-field:focus {
    outline: none;
    background: rgba(77, 143, 171, 0.3);
    box-shadow: inset 0 2px 5px rgba(20, 97, 231, 0.1), 0 0 0 3px rgba(255, 255, 255, 0.4);
  }

  .but {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
  }

  .btn {
    padding: 16px 0;
    border: none;
    border-radius: 30px;
    background: white;
    color: #008ABC;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 4px 12px rgba(0, 138, 197, 0.3);
  }

  .btn:hover {
    background: #E0F7FA;
    transform: translateY(-3px);
    box-shadow: 0 6px 18px rgba(0, 138, 197, 0.4);
  }

  .btn:active {
    transform: translateY(1px);
    box-shadow: 0 2px 8px rgba(0, 138, 197, 0.2);
  }
  .error-message {
  color: #f44336;
  font-size: 14px;
  margin-bottom: 15px;
  text-align: center;
}
</style>